package shopping.demo;

import java.time.LocalDateTime;
import java.util.*;


public class BuyBooks {
    //初始化
    static Book[] books=new Book[4];
    static {
        System.out.println("欢迎光临实验书社");
        books[0]=new Book(1,"红楼梦",50,10);
        books[1]=new Book(2,"水浒传",40,10);
        books[2]=new Book(3,"三国演绎",30,10);
        books[3]=new Book(4,"西游记",20,10);
    }
    public static void main(String[] args) {
        //显示图书
        showBook(books);
        //购物
        Person p=new Person("张三");
        //购买多种图书
        HashMap<Book,Integer> bookList=p.buyBooks(books);
        count(bookList,p);

    }
    //显示图书方法
    public static void showBook(Book[] book){
        System.out.println("编号\t图书名称\t图书价格\t库存数量");
        for(int i=0;i<book.length;i++){
            System.out.println(book[i].getId()+"\t"+book[i].getName()+"\t"+book[i].getPrice()+"\t"+book[i].getStorage());
        }
    }
    //结算  购买多种图书
    //结算多种书
    public static void count(HashMap<Book,Integer> bookList, Person p) {
        double total = 0;
        Set<Book> set=bookList.keySet();
        Random random=new Random();
        int orderNum=random.nextInt();//产生随机数作为订单编号
        System.out.println("订单编号:"+Math.abs(orderNum));
        System.out.println("购物时间:"+LocalDateTime.now());
        System.out.println("会员:"+p.getName());
        for (Book book: set) {
            int number=bookList.get(book);
            total+=book.getPrice()*number;
            System.out.println(book.getName()+",单价"+book.getPrice()+"购买数量"+number+",共"+book.getPrice()*number);
        }
        System.out.println("------------------------------------");
        System.out.println("\t\t总计"+total);
    }
}
